package com.sendi.mobilework.pager.ext;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sendi.mobilework.R;
import com.sendi.mobilework.api.Api;
import com.sendi.mobilework.datamodel.WorkArrange;
import com.sendi.mobilework.global.GlobalContent;
import com.sendi.mobilework.pager.BasePager;
import com.sendi.mobilework.util.CacheUtils;
import com.sendi.mobilework.util.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 值班安排页面
 * Created by Administrator on 2017/3/22.
 */

public class WorkArrangePager extends BasePager {

    private ListView mListView;
    private List<WorkArrange.RecordBean> mList;
    private ArrangeAdapter mAdapter;
    private final String ACTION="findRota";
    private String TAG = "SENDI";

    private Api mApi;

    public WorkArrangePager(Activity activity) {
        super(activity);

        Retrofit mRetrofit = HttpUtils.getRetrofit(GlobalContent.WORK_ARRANGE_SEVER);
        mApi = mRetrofit.create(Api.class);
    }

    @Override
    public void initView() {
        super.initView();
        mTitleText.setText("值班安排");
        View view = View.inflate(mActivity, R.layout.work_arrange_layout, null);
        mListView = (ListView) view.findViewById(R.id.arrange_listview);

        mFrameLayout.addView(view);
    }

    @Override
    public void initData() {
        mList = new ArrayList<>();
        mAdapter = new ArrangeAdapter();
        String cache = CacheUtils.getCache(mActivity, GlobalContent.WORK_ARRANGE_SEVER+ACTION);
        if (!TextUtils.isEmpty(cache)) {
            Gson gson = new Gson();
            mList = gson.fromJson(cache, WorkArrange.class).getRecord();
            if (mList != null) {
                mListView.setAdapter(mAdapter);
            }
        }
        getDatafromServelet();
    }

    public void getDatafromServelet() {
        mApi.getWorkArrange().enqueue(new Callback<WorkArrange>() {
            @Override
            public void onResponse(Response<WorkArrange> response) {
                mList = response.body().getRecord();
                Gson mGson = new Gson();
                if (response.isSuccess()) {
                    mListView.setAdapter(mAdapter);
                    CacheUtils.setCache(mActivity, GlobalContent.WORK_ARRANGE_SEVER+ACTION,
                            mGson.toJson(response.body()));
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i(TAG, "onFailure: "+t.getMessage());
            }
        });
    }


    class ArrangeAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public WorkArrange.RecordBean getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mActivity, R.layout.work_arrange_item, null);
                holder.place = (TextView) convertView.findViewById(R.id.tv_place);
                holder.time = (TextView) convertView.findViewById(R.id.tv_time);
                holder.worker = (TextView) convertView.findViewById(R.id.tv_worker);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.time.setText(mList.get(position).getTime());
            holder.place.setText(mList.get(position).getPlace());
            holder.worker.setText(mList.get(position).getAttendant());
            return convertView;
        }
    }

    class ViewHolder {
        public TextView time;
        public TextView place;
        public TextView worker;
    }
}
